package com.webchat.client.mall.service;


import com.webchat.common.bean.APIResponseBean;
import com.webchat.common.bean.APIResponseBeanUtil;
import com.webchat.common.exception.BusinessException;
import com.webchat.domain.vo.request.mall.MallProductOperateVO;
import com.webchat.domain.vo.request.mall.MallSPURequestVO;
import com.webchat.domain.vo.response.mall.MallSPUDetailVO;
import com.webchat.rmi.pgc.mall.MallProductClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MallProductService {

    @Autowired
    private MallProductClient mallProductClient;

    /**
     * 商品创建、编辑接口
     *
     * @param spuRequest
     * @return
     */
    public Long save(MallSPURequestVO spuRequest) {

        APIResponseBean<Long> responseBean = mallProductClient.save(spuRequest);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 发布商品
     *
     * @param productOperate
     * @return
     */
    public boolean publish(MallProductOperateVO productOperate) {

        APIResponseBean<Boolean> responseBean = mallProductClient.publish(productOperate);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 撤回商品
     *
     * @param productOperate
     * @return
     */
    public boolean invoke(MallProductOperateVO productOperate) {

        APIResponseBean<Boolean> responseBean = mallProductClient.invoke(productOperate);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 删除商品
     *
     * @param productOperate
     * @return
     */
    public boolean delete(MallProductOperateVO productOperate) {

        APIResponseBean<Boolean> responseBean = mallProductClient.delete(productOperate);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }

    /**
     * 商品浏览，商品详情
     *
     * @param productId
     * @param userId
     * @return
     */
    public MallSPUDetailVO detail(Long productId, String userId) {

        APIResponseBean<MallSPUDetailVO> responseBean = mallProductClient.detail(productId, userId);
        if (APIResponseBeanUtil.isOk(responseBean)) {
            return responseBean.getData();
        }
        throw new BusinessException(responseBean.getMsg());
    }
}
